package com.mark.web.data.controller.backend;

import com.mark.common.ServerResponse;
import com.mark.web.data.entity.vo.CategoryVo;
import com.mark.web.data.service.ICategoryService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Description: 管理员-商品种类模块的控制器
 * @Author: Mark
 * @CreateDate: 2018/8/14
 * @Version: 2.0
 * @Copyright : 豆浆油条个人非正式工作室
 */
@Api(tags = "二手-(后台)商品种类")
@RestController
@RequestMapping("/manage/category/")
public class CategoryManageController {

    private final ICategoryService iCategoryService;

    public CategoryManageController(ICategoryService iCategoryService) {
        this.iCategoryService = iCategoryService;
    }

    @RequestMapping("add_category")
    @ResponseBody
    public ServerResponse<String> addCategory(String categoryName, Integer parentId, String imageAddress) {
        if (parentId == null || StringUtils.isBlank(categoryName) || StringUtils.isBlank(imageAddress)) {
            return ServerResponse.createByErrorMessage("添加失败！提交的数据有空值");
        }
        iCategoryService.addCategory(categoryName, parentId, imageAddress);
        return ServerResponse.createBySuccessMessage("添加商品种类成功！");
    }

    //2.更新商品种类名
    @RequestMapping("update_category_name")
    @ResponseBody
    public ServerResponse<String> updateCategoryName(Integer categoryId, String CategoryNewName) {
        iCategoryService.updateCategoryName(categoryId, CategoryNewName);
        return ServerResponse.createBySuccessMessage("更新商品种类名成功！");
    }

    @RequestMapping("get_category")
    @ResponseBody
    public ServerResponse<List<CategoryVo>> getChildrenParallelCategory(HttpSession session, @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId) {
        List<CategoryVo> data = iCategoryService.getChildrenParallelCategory(categoryId);
        return ServerResponse.createBySuccess(data);
    }

    @RequestMapping("get_deep_category.do")
    @ResponseBody
    public ServerResponse<List<Integer>> getCategoryAndDeepChildrenCategory(@RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId) {
        List<Integer> data = iCategoryService.selectCategoryAndChildrenById(categoryId);
        return ServerResponse.createBySuccess(data);
    }
}
